var express = require('express');
var router = express.Router();
const mongoose = require('mongoose');
mongoose.connect('mongodb://www.996day.com:27017/todolists', { useNewUrlParser: true });
//创建一个登录账号验证
const login = new mongoose.model('login', {
    username: String,
    password: Number
});
//创建代办事项
let todos = new mongoose.model('todolist', {
    id: Number,
    title: String,
    isCompleted: Boolean
});
let atodos = ""
let bl = false
let nn = ""
    //注册账号
router.post('/register', function(req, res, next) {
    const { username, password } = req.body;
    let name = new login({ username, password });
    name.save().then(data => {
        res.json({
            status: 1,
            code: 200,
            data
        })
    }).catch(err => {
        res.json({
            status: 0,
            code: 200,
            data: '账号重复',
            err
        })
    })
});
//登录账号
router.post('/login', function(req, res, next) {
    const { username, password } = req.body;
    login.find({ username }).then(data => {
        if (data.length === 1) {
            if (data[0].password == password) {
                bl = true
                if (atodos === "" || nn !== data[0].username) {
                    atodos = new mongoose.model('todolist' + data[0].username, {
                        id: Number,
                        title: String,
                        isCompleted: Boolean
                    })
                }
                nn = data[0].username
                res.json({
                    status: 1,
                    code: 200,
                    data,
                    name: username
                })
            } else {
                res.json({
                    status: 0,
                    code: 200,
                    data: "账号或密码错误"
                })
            }
        } else {
            res.json({
                status: 0,
                code: 200,
                data: "账号或密码错误"
            })
        }
    })
});
//根据名字请求数据
router.post('/namelogin', function(req, res, next) {
    atodos.find()
        .then(data => {
            res.json({
                code: 200,
                data
            })
        })
});
//重置为公用平台
router.post('/public', function(req, res, next) {
    if (bl) {
        bl = false
    }
    todos.find()
        .then(data => {
            res.json({
                code: 200,
                data,
                name: '当前属于公用平台'
            })
        })
});
//保存到数据库
router.post('/settodos', function(req, res, next) {
    const { id, title, isCompleted } = req.body;
    if (bl) {
        let user = new atodos({ id, title, isCompleted });
        user.save().then(data => {
            res.json(data)
        }).catch()
    } else {
        let user2 = new todos({ id, title, isCompleted });
        user2.save().then(data => {
            res.json(data)
        }).catch()
    }

});
router.post('/gettodos', function(req, res, next) {
    if (bl) {
        atodos.find()
            .then(data => {
                res.json({
                    code: 200,
                    data,
                    name: nn
                })
            })
    } else if (!bl) {
        todos.find()
            .then(data => {
                res.json({
                    code: 200,
                    data,
                    name: '当前属于公用平台'
                })
            })
    }
});
router.post('/removetodos', function(req, res, next) {
    if (bl == true) {
        atodos.deleteOne({ _id: req.body.id }, () => {
            res.json({
                code: 200,
                data: '删除成功'
            })
        })
    } else {
        todos.deleteOne({ _id: req.body.id }, () => {
    // console.log(req.body[0]._id)
        res.json({
                code: 200,
                data: '删除成功'
            })
        })
    }
});
router.post('/findtodos', function(req, res, next) {
    let { title, isCompleted, _id } = req.body
    if (bl) {
        atodos.update({ _id: _id }, { $set: { 'isCompleted': isCompleted } }).then(daa => {
            res.json({
                code: 200,
                data: '修改成功'
            })
        })
    } else {
        todos.update({ _id: _id }, { $set: { 'isCompleted': isCompleted } }).then(daa => {
            res.json({
                code: 200,
                data: '修改成功'
            })
        })
    }
})

module.exports = router;